
package result;

import com.alibaba.fastjson.JSON;
import enums.ErrorCode;


/**
 * ClassName:Result <br/>
 * Date: 2018年10月21日 下午12:55:29 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
public class Result<T> {

    private int    code;
    private String desc;
    private T      data;
    private ErrorCode errorCode;

    public static <T> Result<T> success(T data) {
        Result<T> rtn = new Result<T>(ErrorCode.SUCCESS);
        rtn.setData(data);
        return rtn;
    }

    public static <T> Result<T> success() {
        Result<T> rtn = new Result<T>(ErrorCode.SUCCESS);
        return rtn;
    }

    public static <T> Result<T> fail(ErrorCode errorCode) {
        Result<T> rtn = new Result<T>(errorCode);
        return rtn;
    }

    public static <T> Result<T> fail(Integer code, String message) {
        Result<T> rtn = new Result<T>();
        rtn.setCode(code);
        rtn.setDesc(message);
        return rtn;
    }

    public Result() {
    }

    public Result(ErrorCode errorCode) {
        super();
        this.errorCode = errorCode;
        this.code = errorCode.getCode();
        this.desc = errorCode.getMessage();
    }

//    public int getCode() {
//        return errorCode.getCode();
//    }
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String toString() {
        return JSON.toJSONString(this);
    }

}
